package toy.keli.edic.ui.main.components

import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.material.MaterialTheme
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.view.setPadding
import toy.keli.edic.R
import toy.keli.edic.data.model.Article
import toy.keli.edic.ui.level.LevelViewModel
import toy.keli.edic.view.AutoLinefeedLayout


/**
 * 级别弹框
 */
fun TopDialog(name:String,word:String,onWordClick:(word:String)->Unit={}){
    val title = name.substring(0, 4)
    if(!title.startsWith("Top")) return
    TopInfo(title){ info->
        val more = if (info.inSize > 0) "累加到该级别共有${info.inSize}个单词，到现在为止一篇文章中${info.inScale}的单词你都认识。" else ""
        AlertDialog({
            this.title = title
            this.okText = "关闭"
            val body =  "本级别包含单词${info.size}个，出现概率为${info.scale},平均${info.score}个单词就会出现一个。" + more
            this.layout = R.layout.top_info
            onLayoutInit = {
                findViewById<TextView>(R.id.top_msg).apply {
                    text =body
                }
                findViewById<AutoLinefeedLayout>(R.id.top_data).apply {
                    info.words.forEach({ word->
                        addView(TextView(context).apply{
                            text =word
                            setTextColor(Color.Blue.toArgb())
                            setPadding(5)
                            setOnClickListener {
                                onWordClick(word)
                                InfoDialog.close()
                            }
                        })
                    })
                }
            }
        })
//        {
//            FlowRow(
////                mainAxisSpacing = 8.dp, // 水平间距
////                crossAxisSpacing = 8.dp // 垂直间距
//            ) {
//                info.words.forEach {
//                    val tColor =
//                        if (it.equals(word)) Color.Red else MaterialTheme.colorScheme.primary
//                    Text(
//                        text = it, color = tColor, fontSize = 14.sp, modifier = Modifier
//                            .padding(horizontal = 5.dp)
//                            .clickable {
//                                onWordClick(it)
//                                InfoDialog.close()
//                            }
//                    )
//                }
//            }
//        }

    }
}

class TopInfo(val name:String,var info:List<String> =listOf<String>(),var onLoadFinish:(TopInfo)->Unit={}){
    var words =listOf<String>()
    init{
        val info = LevelViewModel.getTopInfo(name)
        this.info =  info.substring(1,info.length-1).split(Regex(",|:")).map{it.trim()}
        words = Article.getTop(name.substring(name.length-1).toInt()).sorted()
        onLoadFinish(this)
    }
    //数量
    val size:Int
        get() = info[0].toInt()
    //概率百分比
    val scale:String
        get() = info[1]
    //几个单词会出现一个
    val score:Int
        get() = info[2].split("/").get(1).toInt()
    //累加数量
    val inSize:Int
        get() = if(info.size>3) info[3].toInt() else 0
    //累加百分比
    val inScale:String
        get() = if(info.size>3) info[4] else ""
}

